﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServiceInstaller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
        }

        private void btnInstallService_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<object, object> savedState = new Dictionary<object, object>();
                using (AssemblyInstaller serviceInstaller = new AssemblyInstaller())
                {
                    serviceInstaller.UseNewContext = true;
                    serviceInstaller.Path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "GPDWin4GameBarPowerBackend.exe");
                    serviceInstaller.Install(savedState);
                    MessageBox.Show("安装成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        const string serviceName = "GPDWinGameBarPowerBackend";

        private void btnStartService_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Stopped)
                    {
                        control.Start();
                        MessageBox.Show("服务已启动");
                    }
                    else
                    {
                        MessageBox.Show("服务正在运行");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Running)
                    {
                        control.Stop();
                        MessageBox.Show("服务已停止");
                    }
                    else
                    {
                        MessageBox.Show("服务没有运行");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUninstallService_Click(object sender, EventArgs e)
        {
            Dictionary<object, object> savedState = new Dictionary<object, object>();
            try
            {
                using (AssemblyInstaller serviceInstaller = new AssemblyInstaller())
                {
                    serviceInstaller.UseNewContext = true;
                    serviceInstaller.Path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "GPDWin4GameBarPowerBackend.exe");
                    serviceInstaller.Uninstall(savedState);
                    MessageBox.Show("卸载成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
